package edu.hawaii.teams.halealohacli;

import java.util.Scanner;
import org.wattdepot.client.WattDepotClient;
import edu.hawaii.teams.halealohacli.processor.Processor;

/**
 * Command-line interface for the hale-aloha-cli-teams application.
 * 
 * @author Jayson T. Gamiao
 * 
 */
public class Main {
  /**
   * main serves as a driver for the command-line interface.
   * 
   * @param args URL to a running WattDepot server.
   * @throws Exception any errors that occur.
   */
  public static void main(String[] args) throws Exception {
    Scanner scan = null;
    String date = null;
    String prompt = "\n> ";
    String selection = null;
    String tower = null;
    // The URL of the Hale Aloha server
    String url = "http://server.wattdepot.org:8190/wattdepot/";
    // Create a client.
    WattDepotClient client = new WattDepotClient(url);
    // Create a Processor object used to process input commands.
    Processor processor = new Processor(client);
    // Check to make sure a connection can be made.
    // If no connection, then exit right now.
    if (client.isHealthy()) {
      System.out.println("Connected successfully to the Hale Aloha WattDepot server.");
      System.out.print("> ");
      do {
        scan = new Scanner(System.in);
        selection = scan.next();
        if (selection.equalsIgnoreCase("current-power")) {
          tower = scan.next();
          processor.parseCurrentPower("current-power " + tower);
          System.out.print(prompt);
        }
        else if (selection.equalsIgnoreCase("daily-energy")) {
          tower = scan.next();
          date = scan.next();
          processor.parseDailyEnergy("daily-energy " + tower + " " + date);
          System.out.print(prompt);
        }
        else if (selection.equalsIgnoreCase("energy-since")) {
          tower = scan.next();
          date = scan.next();
          processor.parseEnergySince("energy-since " + tower + " " + date);
          System.out.print(prompt);
        }
        else if (selection.equalsIgnoreCase("rank-towers")) {
          tower = scan.next();
          date = scan.next();
          processor.parseRankTowers("rank-towers " + tower + " " + date);
          System.out.print(prompt);
        }
        else if (selection.equalsIgnoreCase("set-baseline")) {
          tower = scan.next();
          date = scan.nextLine();
          processor.parseSetBaseLine("set-baseline " + tower + " " + date.trim());
          System.out.print(prompt);
        }
        else if (selection.equalsIgnoreCase("monitor-power")) {
          tower = scan.next();
          String interval = scan.nextLine();
          processor.parseMonitorPower("monitor-power " + tower + " " + interval.trim());
          System.out.print(prompt);
        }
        else if (selection.equalsIgnoreCase("monitor-goal")) {
          tower = scan.next();
          String goal = scan.next();
          String interval = scan.nextLine();
          processor.parseMonitorGoal("monitor-goal " + tower + " " + goal + " " + interval.trim());
          System.out.print(prompt);
        }
        else if (selection.equalsIgnoreCase("help")) {
          help();
          System.out.print(prompt);
        }
        else if (selection.equalsIgnoreCase("quit")) {
          System.out.println();
        }
        else {
          System.out.println("Command not recognized. Please try again.");
          System.out.print("> ");
        }
      }
      // Quit the application.
      while (!selection.equalsIgnoreCase("quit"));
    }
    else {
      System.out.format("Could not connect to: %s%n", url);
      return;
    }
  }

  /**
   * Provides instructions on how to use this system.
   */
  public static void help() {
    System.out.println("Here are the available commands for this system.\n");
    System.out.println("current-power [tower | lounge]");
    System.out.println("  Returns the current power in kW for the associated tower or lounge.\n");
    System.out.println("daily-energy [tower | lounge] [date]");
    System.out
        .println("  Returns the energy in kWh used by the tower or lounge for the specified date "
            + "(yyyy-mm-dd).\n");
    System.out.println("energy-since [tower | lounge] [date]");
    System.out.println("  Returns the energy used since the date (yyyy-mm-dd) to now.\n");
    System.out.println("rank-towers [start] [end]");
    System.out
        .println("  Returns a list in sorted order from least to most energy consumed between the"
            + " [start] and [end] date (yyyy-mm-dd)\n");
    
    System.out.println("set-baseline [tower | lounge] [date] (optional)");
    System.out.println("  Sets the day to save the 24 hour " + 
                       "period energy level of a tower\n");
    System.out.println("monitor-power [tower | lounge] [interval] (optional)");
    System.out.println("  Returns the current power being consumed at the source at each " +
        "interval.\n");
    System.out.println("monitor-goal [tower | lounger] [goal] [interval]");
    System.out.println("  Returns the current power consumed at the source at each interval and");
    System.out.println("  displays whether or not the source is meeting its power conservation " +
        "goal\n");
    System.out.println("quit");
    System.out.println("  Terminates execution\n");
    System.out.println("Note: towers are:  Mokihana, Ilima, Lehua, Lokelani");
    System.out
        .println("Lounges are the tower names followed by a \"-\" followed by one of A, B, C, D, E"
            + ". For example, Mokihana-A");
  }
}